<?php

require_once(dirname(__DIR__) . '/api/DB.php');
require_once(dirname(__DIR__) . '/api/response.php');
require_once(dirname(__DIR__) . '/api/func.php');

try {

    $db = new DB();
    $pdo = $db::$pdo;

    if (!isset($_FILES['file'])) {
        throw new Exception(fieldMap('file') . '为必传项');
    };

    if (empty($_FILES['file'])) {
        throw new Exception(fieldMap('file') . '不能为空');
    }

    // 上传文件
    $file = $_FILES['file'];
    $fileFormat = pathinfo($file['name'], PATHINFO_EXTENSION);
    $path = '/uploadFile/' . md5(uniqid()) . ".{$fileFormat}";
    $fullPath = dirname(__DIR__) . $path;
    if (!move_uploaded_file($file['tmp_name'], $fullPath)) {
        throw new Exception('文件上传失败');
    }

    $scheme = $_SERVER['REQUEST_SCHEME'];
    $usePath = "{$scheme}://" . $_SERVER['HTTP_HOST'] . $path;

    myResponse('', 1, 200, $usePath);
} catch (Exception $e) {
    myResponse($e->getMessage(), 0, 200);
}
